package Operations;

import Book.BookList;
import Book.Book;

import java.util.Scanner;

public class AddOperation implements Operation{

    @Override
    public void operation(BookList bookList) {

        int currentSize = bookList.getUsedSize();//存储当前书的数目

        if (currentSize == bookList.getBooks().length) {
            System.out.println("书架已满，无法放书");
        }

        Scanner scanner = new Scanner(System.in);

        System.out.println("添加图书");

        System.out.println("输入书名");
        String name = scanner.nextLine();

        for (int i = 0; i < currentSize; i++) {//判断有没有名字相同的书
            if (name .equals(bookList.getBooks()[i].getName())) {
                System.out.println("已有这本书，无法插入");
                return;
            }
        }

        System.out.println("输入作者");
        String author = scanner.nextLine();

        System.out.println("输入价格");
        double price = scanner.nextDouble();

        scanner.nextLine();

        System.out.println("输入类型");
        String type = scanner.nextLine();

        Book book = new Book(name, author, price, type);

        bookList.setBooks(currentSize++, book);

        bookList.setUsedSize(currentSize);

        new ShowOperation().operation(bookList);

        System.out.println("添加成功");
    }
}
